package cn.com.inossem.utils;

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

/**
 * 时间工具类
 *
 * @author wuyt3
 */
public class TimeUtil {
	@SuppressLint("SimpleDateFormat")
	public static DateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
	@SuppressLint("SimpleDateFormat")
	public static DateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
	@SuppressLint("SimpleDateFormat")
	public static DateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日 E"); // E表示星期几
	@SuppressLint("SimpleDateFormat")
	public static DateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 精确到小时
	@SuppressLint("SimpleDateFormat")
	public static DateFormat sdf5 = new SimpleDateFormat("yyyy.MM.dd");
	@SuppressLint("SimpleDateFormat")
	public static DateFormat sdf6 = new SimpleDateFormat("yyyy-M-d");
	private static String currTime;

	public static String parseDate(DateFormat df, String date) {

		try {
			if (df.equals(sdf2) || df.equals(sdf3)) {
				return df.format(stringtoDate(date));
			} else {
				return df.format(df.parse(date));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 获取当前日是星期几
	 */
	public static int getWeekday() {
		Calendar c = Calendar.getInstance();
		int weekday = c.get(Calendar.DAY_OF_WEEK);
		System.out.println("今天是星期" + (weekday == 1 ? 7 : weekday - 1));
		return weekday == 1 ? 7 : weekday - 1;
	}

	public static String getCurrTime() {
		return new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA).format(new Date());
	}

	private int getMondayPlus() {
		Calendar calendar = Calendar.getInstance();
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		if (dayOfWeek == 1) {
			return -6;
		} else {
			return 2 - dayOfWeek;
		}
	}

	/**
	 * 获取当前的
	 *
	 * @return
	 */
	public String getCurrentMonday() {
		// weeks = 0;
		int mondayPlus = this.getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus);
		Date monday = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	/**
	 * 日期转换为星期
	 *
	 * @param mdate 日期
	 * @return
	 */
	public static List<Date> dateToWeek(Date mdate) {
		System.out.println("mdate :" + mdate);
		int day = mdate.getDay();
		System.out.println("b:  " + day);
		Date fdate;
		List<Date> list = new ArrayList();
		Long fTime = mdate.getTime() - day * 24 * 3600000;
		System.out.println("fTime: " + fTime);
		for (int i = 0; i < 8; i++) {
			fdate = new Date();
			if (i == 0) {
				fdate.setTime(fTime + (i + 1 * 24 * 3600000));
				list.add(i, fdate);

			} else {
				fdate.setTime(fTime + (i * 24 * 3600000));
				list.add(i, fdate);
			}

		}

		return list;
	}

	/**
	 * long型的时间转化为字符串
	 *
	 * @param time
	 * @return
	 */
	public static String long2String(long time) {

		return sdf1.format(new Date(time));
	}

	/**
	 * 字符串转换为日期
	 *
	 * @param dateStr 字符串的格式的日期
	 * @return
	 */
	public static Date stringtoDate(String dateStr) {
		Date date = null;

		try {
			sdf1.setLenient(false);// 指定日期/时间分析是否不严格 为true时为不严格 ，否则为严格匹配该日期格式
			date = sdf1.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();

		}
		return date;
	}

	/**
	 * 日期转换为字符串
	 *
	 * @param date 日期
	 * @return
	 */
	public static String dateToString(java.util.Date date) {
		String result = "";

		try {
			result = sdf1.format(date);
		} catch (Exception e) {

		}
		return result;
	}

	/**
	 * 日期转换为字符串 如yyyy年MM月dd日
	 *
	 * @param date 日期
	 * @return
	 */
	public static String dateToString1(java.util.Date date) {
		String result = "";

		try {
			result = sdf2.format(date);
		} catch (Exception e) {

		}
		return result;
	}

	public static String getToday() {
		Date date = new Date();
		return sdf5.format(date);
	}

	public static String getDay(Date date) {
		return sdf5.format(date);
	}

	public static String getDateStr(String time) {
		try {
			Date date = sdf6.parse(time);
			return getWeek(sdf5.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getWeek(String date) {
		try {
			Calendar c = Calendar.getInstance();
			c.setTime(sdf5.parse(date));
			if (c.get(Calendar.DAY_OF_WEEK) == 1) {
				return date + " 星期天";
			}
			if (c.get(Calendar.DAY_OF_WEEK) == 2) {
				return date + " 星期一";
			}
			if (c.get(Calendar.DAY_OF_WEEK) == 3) {
				return date + " 星期二";
			}
			if (c.get(Calendar.DAY_OF_WEEK) == 4) {
				return date + " 星期三";
			}
			if (c.get(Calendar.DAY_OF_WEEK) == 5) {
				return date + " 星期四";
			}
			if (c.get(Calendar.DAY_OF_WEEK) == 6) {
				return date + " 星期五";
			}
			if (c.get(Calendar.DAY_OF_WEEK) == 7) {
				return date + " 星期六";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date getDate(String datStr) {
		try {
			return sdf5.parse(datStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}